package com.vcredit.creditcat.time.admin;

import com.alibaba.dubbo.common.threadpool.RequestLocalThread;
import com.vcredit.creditcat.manager.ChannelExternalManager;
import com.vcredit.creditcat.util.ConcurrentDateUtil;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.IJobHandler;
import com.xxl.job.core.handler.annotation.JobHander;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;

import java.util.Date;

/*@QuartzJob(name = "chinaleScueTime", cron = "0 0 0 * * ?")*/
@JobHander(value = "channelExternalTime")
@Component
public class ChannelExternalTime extends IJobHandler {
	// 0 0/15 * * * ? 15分钟一次 0 0 0 * * ? 12点开始
	private static Logger log = LoggerFactory.getLogger(ChannelExternalTime.class);
	@Autowired
	private ChannelExternalManager channelExternalManager;
	@Autowired
	private ThreadPoolTaskExecutor threadPoolTaskExecutor;

	@Override
	public ReturnT<String> execute(String... strings) throws Exception {
		log.info("产品统计自动生成任务开始................!!" + ConcurrentDateUtil.formatDateTime(new Date()));
		RequestLocalThread.create();
		try {
			threadPoolTaskExecutor.execute(new CrawleThread());
			return ReturnT.SUCCESS;
		} catch (Exception e) {
			log.error(e.getMessage());
			return ReturnT.FAIL;
		}
	}

	class CrawleThread implements Runnable {
		private Logger CrawleLOG = LoggerFactory.getLogger(CrawleThread.class);

		@Override
		public void run() {
			try {
				channelExternalManager.excute();
			} catch (Exception e) {
				CrawleLOG.error(e.getStackTrace().toString());
			}
		}
	}
}